package com.xidian.zhiancloud.controller.chemicalsManagement.dangerousPart;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.DangerousChemicalReviewRecord;
import com.xidian.zhiancloud.model.DangerousChemicalWarehouseOut;
import com.xidian.zhiancloud.qo.chemicalsManagement.ApplyQO;
import com.xidian.zhiancloud.qo.chemicalsManagement.DangerousChemicalWarehouseQO;
import com.xidian.zhiancloud.service.chemicalsManagement.chemicalManager.dangerousPart.DangerousChemicalReviewService;
import com.xidian.zhiancloud.service.chemicalsManagement.chemicalManager.dangerousPart.DangerousChemicalWarehouseOutService;
import com.xidian.zhiancloud.service.user.WorkFlowUserService;
import com.xidian.zhiancloud.vo.chemicalsManagement.DangerousChemicalWarehouseOutVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 危化品管理 危化品库存管理  申请|出库|使用|归还
 * @Author: Egoist
 * @Date: 2020/11/10 17:31
 */
@RestController
@RequestMapping("/DangerousChemicalWarehouseOut")
public class DangerousChemicalWarehouseOutController extends BaseController {

    @Autowired
    private DangerousChemicalWarehouseOutService dangerousChemicalWarehouseOutService;
    @Autowired
    private WorkFlowUserService workFlowUserService;
    @Autowired
    private DangerousChemicalReviewService dangerousChemicalReviewService;

    /**
     * 增加危化品出库记录
     *
     * @param dangerousChemicalWarehouseOut 危化品出库对象
     * @return  返回是否添加成功
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> add(DangerousChemicalWarehouseOut dangerousChemicalWarehouseOut) {
        dangerousChemicalWarehouseOut.setApplyTime(new Date(System.currentTimeMillis()));
        dangerousChemicalWarehouseOut.setCompanyId(getSessionUserCompanyId());
        dangerousChemicalWarehouseOut.setApplicantId(getSessionUserid());
        //添加审核人
        List<Long> reviewerIds =workFlowUserService.getByChemicalReviewerAndJobId (getSessionUserCompanyId()) ;
        StringBuilder stringBuilder = new StringBuilder();
        for (Long id : reviewerIds) {
            stringBuilder.append(id).append(',');
        }
        String reviewerStrIds = stringBuilder.toString();
        reviewerStrIds = reviewerStrIds.substring(0, reviewerStrIds.length() - 1);
        dangerousChemicalWarehouseOut.setReviewerIds(reviewerStrIds);
        return ResultDTO.successOf(dangerousChemicalWarehouseOutService.save(dangerousChemicalWarehouseOut), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id获取危化品出库记录
     *
     * @param id 危化品出库对象的id
     * @return 返回危化品出库对象
     */
    @GetMapping("/getById")
    public ResultDTO<DangerousChemicalWarehouseOutVO> getById(Long id) {
        return ResultDTO.successOf(dangerousChemicalWarehouseOutService.getVOById(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id删除危化品出库记录
     *
     * @param ids ids数组
     * @return 返回是否删除成功
     */
    @PostMapping("/deleteByIds")
    public ResultDTO<Boolean> deleteByIds(Long[] ids) {
        return ResultDTO.successOf(dangerousChemicalWarehouseOutService.removeByIds(Arrays.asList(ids)), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id修改危化品出库记录
     *
     * @param dangerousChemicalWarehouseOut 危化品出库对象那个
     * @return 返回是否修改成功
     */
    @PostMapping("/patchById")
    public ResultDTO<Boolean> patchById(DangerousChemicalWarehouseOut dangerousChemicalWarehouseOut) {
        dangerousChemicalWarehouseOut.setCompanyId(null);
        dangerousChemicalWarehouseOut.setApplicantId(null);
        Long patchUserId = getSessionUserid();
        return ResultDTO.successOf(dangerousChemicalWarehouseOutService.patchById(dangerousChemicalWarehouseOut, patchUserId), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 领用申请时进行提交
     *
     * @param id 申请对象id
     * @return 是否提交成功
     */
    @PostMapping("/submitById")
    public ResultDTO<Boolean> submitById(Long id) {
        return dangerousChemicalWarehouseOutService.submitById(id);
    }

    /**
     * 对申请进行出库
     * @param dangerousChemicalWarehouseOut 出库对象
     * @return 是否成功发放
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    @PostMapping("/distributionById")
    public ResultDTO<Boolean> distributionById(DangerousChemicalWarehouseOut dangerousChemicalWarehouseOut) {
        dangerousChemicalWarehouseOut.setCompanyId(getSessionUserCompanyId());
        return dangerousChemicalWarehouseOutService.distributionById(dangerousChemicalWarehouseOut);
    }

    /**
     * 对使用信息进行录入
     * @param dangerousChemicalWarehouseOut 使用信息对象
     * @return 是否成功录入
     */
    @PostMapping("/usageEntryById")
    public ResultDTO<Boolean> usageEntryById(DangerousChemicalWarehouseOut dangerousChemicalWarehouseOut) {
        dangerousChemicalWarehouseOut.setCompanyId(getSessionUserCompanyId());
        return dangerousChemicalWarehouseOutService.usageEntryById(dangerousChemicalWarehouseOut);
    }

    /**
     * 对申请进行归还
     * @param dangerousChemicalWarehouseOut 出库对象
     * @return 是否成功归还
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    @PostMapping("/returnById")
    public ResultDTO<Boolean> returnById(DangerousChemicalWarehouseOut dangerousChemicalWarehouseOut) {
        Long userId = getSessionUserid();
        return ResultDTO.successOf(dangerousChemicalWarehouseOutService.returnById(dangerousChemicalWarehouseOut, userId), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 获得危化品库存名称列表
     * @return 危化品库存名称列表
     */
    @GetMapping("/getStorageNameList")
    public ResultDTO<Map<Long, String>> getStorageNameList() {
        Long companyId = getSessionUserCompanyId();
        return ResultDTO.successOf(dangerousChemicalWarehouseOutService.getStorageNameList(companyId), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 危化品出库审核记录
     *
     * @param id 出库记录id
     * @return 危化品出库审核记录
     */
    @GetMapping("/getReviewRecordById")
    public ResultDTO<List<DangerousChemicalReviewRecord>> getReviewRecordById(Long id){
        return ResultDTO.successOf(dangerousChemicalReviewService.getReviewRecordById(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 申请列表选择品名后得到管辖部门列表
     * @param qo 查询对象
     * @return 管辖部门列表
     */
    @GetMapping("/getApplyOrgList")
    public ResultDTO<Map<Long, String>> getApplyOrgList(ApplyQO qo) {
        qo.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(dangerousChemicalWarehouseOutService.getApplyOrgList(qo), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 申请列表选择品名和管辖部门后查询存放地点列表
     * @param qo 查询对象
     * @return 存放地点列表
     */
    @GetMapping("/getApplyPosList")
    public ResultDTO<Map<Long, String>> getApplyPosList(ApplyQO qo) {
        qo.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(dangerousChemicalWarehouseOutService.getApplyPosList(qo), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 申请列表选择品名、管辖部门、查询存放地点后查询负责人列表
     * @param qo 查询对象
     * @return 负责人列表
     */
    @GetMapping("/getApplyMagList")
    public ResultDTO<Map<Long, String>> getApplyMagList(ApplyQO qo) {
        qo.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(dangerousChemicalWarehouseOutService.getApplyMagList(qo), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 获取危化品库存id
     * @param qo 查询对象
     * @return 危化品库存id
     */
    @GetMapping("/getStorageId")
    public ResultDTO<Long> getStorageId(ApplyQO qo) {
        qo.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(dangerousChemicalWarehouseOutService.getStorageId(qo), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response, Long[] ids) throws IOException {
        ResultDTO.successOf(dangerousChemicalWarehouseOutService.export(response, ids), GeneralResponseEnums.SUCCESS);
    }


    /**
     * 台账列表查询
     *
     * @param dangerousChemicalWarehouseQO 请求参数
     * @return 返回出库台账列表
     */
    @GetMapping("/getAccountListByQueryObject")
    public ResultDTO<IPage<DangerousChemicalWarehouseOutVO>> getAccountListByQueryObject(DangerousChemicalWarehouseQO dangerousChemicalWarehouseQO){
        if (dangerousChemicalWarehouseQO.getCurrent() == null || dangerousChemicalWarehouseQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        dangerousChemicalWarehouseQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(dangerousChemicalWarehouseOutService.getListByQO(dangerousChemicalWarehouseQO, 0), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 领用申请列表查询
     *
     * @param dangerousChemicalWarehouseQO 请求参数
     * @return 返回领用申请信息列表
     */
    @GetMapping("/getApplyListByQueryObject")
    public ResultDTO<IPage<DangerousChemicalWarehouseOutVO>> getApplyListByQueryObject(DangerousChemicalWarehouseQO dangerousChemicalWarehouseQO){
        if (dangerousChemicalWarehouseQO.getCurrent() == null || dangerousChemicalWarehouseQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        dangerousChemicalWarehouseQO.setApplicantId(getSessionUserid());
        dangerousChemicalWarehouseQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(dangerousChemicalWarehouseOutService.getListByQO(dangerousChemicalWarehouseQO, 1), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 领用审批列表查询
     *
     * @param dangerousChemicalWarehouseQO 请求参数
     * @return 返回领用审批信息列表
     */
    @GetMapping("/getReviewListByQueryObject")
    public ResultDTO<IPage<DangerousChemicalWarehouseOutVO>> getReviewListByQueryObject(DangerousChemicalWarehouseQO dangerousChemicalWarehouseQO){
        if (dangerousChemicalWarehouseQO.getCurrent() == null || dangerousChemicalWarehouseQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        dangerousChemicalWarehouseQO.setReviewerId(getSessionUserid());
        dangerousChemicalWarehouseQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(dangerousChemicalWarehouseOutService.getListByQO(dangerousChemicalWarehouseQO, 2), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 危化品出库列表查询
     *
     * @param dangerousChemicalWarehouseQO 请求参数
     * @return 返回危化品出库列表
     */
    @GetMapping("/getOutListByQueryObject")
    public ResultDTO<IPage<DangerousChemicalWarehouseOutVO>> getOutListByQueryObject(DangerousChemicalWarehouseQO dangerousChemicalWarehouseQO){
        if (dangerousChemicalWarehouseQO.getCurrent() == null || dangerousChemicalWarehouseQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        dangerousChemicalWarehouseQO.setOuterId(getSessionUserid());
        dangerousChemicalWarehouseQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(dangerousChemicalWarehouseOutService.getListByQO(dangerousChemicalWarehouseQO, 3), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 危化品使用列表查询
     *
     * @param dangerousChemicalWarehouseQO 请求参数
     * @return 返回危化品使用列表
     */
    @GetMapping("/getUseListByQueryObject")
    public ResultDTO<IPage<DangerousChemicalWarehouseOutVO>> getUseListByQueryObject(DangerousChemicalWarehouseQO dangerousChemicalWarehouseQO){
        if (dangerousChemicalWarehouseQO.getCurrent() == null || dangerousChemicalWarehouseQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        dangerousChemicalWarehouseQO.setApplicantId(getSessionUserid());
        dangerousChemicalWarehouseQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(dangerousChemicalWarehouseOutService.getListByQO(dangerousChemicalWarehouseQO, 4), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 危化品归还列表查询
     *
     * @param dangerousChemicalWarehouseQO 请求参数
     * @return 返回危化品归还列表
     */
    @GetMapping("/getReturnListByQueryObject")
    public ResultDTO<IPage<DangerousChemicalWarehouseOutVO>> getReturnListByQueryObject(DangerousChemicalWarehouseQO dangerousChemicalWarehouseQO){
        if (dangerousChemicalWarehouseQO.getCurrent() == null || dangerousChemicalWarehouseQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        dangerousChemicalWarehouseQO.setOuterId(getSessionUserid());
        dangerousChemicalWarehouseQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(dangerousChemicalWarehouseOutService.getListByQO(dangerousChemicalWarehouseQO, 5), GeneralResponseEnums.SUCCESS);
    }

}
